package com.away.ymcc.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.aliyun.sdk.service.dysmsapi20170525.*;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;


public class SendSmsUtil {
    public static void send(String phone,String code){

        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("LTAI5tPy5vVSW8zFmnPAJ4a2")
                .accessKeySecret("zkNk2Bd3p96KyBk4VowcwzgDwuflIp")
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-qingdao") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",code);
        String jsonString = jsonObject.toJSONString();

        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName("阿里云通信")
                .templateCode("SMS_322205777")
                .phoneNumbers(phone)
                .templateParam(jsonString)
                .build();

        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = null;
        try {
            resp = response.get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(new Gson().toJson(resp));

        client.close();
    }

}
